<template>
  <div>
    <!--面包屑-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>报告管理</el-breadcrumb-item>
      <el-breadcrumb-item>测试报告</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card class="box-card">
      <el-row :gutter="20">
        <!--搜索框-->
        <el-col :span="6">
          <el-input placeholder="请输入报告名称" v-model="queryParams.name" clearable @clear="getReportList" @keyup.enter.native="getReportList">
            <el-button slot="append" icon="el-icon-search" @click="getReportList" class="search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!--表格-->
      <el-table :data="reportList" :cell-style="cellStyle" border stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="报告名称" prop="name"></el-table-column>
        <el-table-column label="运行结果" prop="result" width="100px"></el-table-column>
        <el-table-column label="成功" prop="success" width="80px"></el-table-column>
        <el-table-column label="失败" prop="fail" width="80px"></el-table-column>
        <el-table-column label="错误" prop="error" width="80px"></el-table-column>
        <el-table-column label="跳过" prop="skip" width="80px"></el-table-column>
        <el-table-column label="用例总数" prop="count" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="create_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="openReportHtml(scope.row.id)" plain>打 开</el-button>
            <el-button type="warning" size="mini" @click="downloadReportHtml(scope.row.id, scope.row.name)" plain>下 载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.p"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="queryParams.s"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!--报告详情对话框-->
    <el-dialog title="测试报告详情" :visible.sync="reportContentDialogVisible" width="60%" :append-to-body="true"
               :close-on-click-modal="false" :close-on-press-escape="false" @close="reportContentClose">
      <div class="report" v-html="reportHtml" width="100%" height="100%"></div>
      <span slot="footer" class="dialog-footer">
          <el-button @click="reportContentDialogClose">关 闭</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import { getReports, getReportHtml, downloadReportHtml } from '../../api/api'

export default {
  created () {
    this.getReportList()
  },
  data () {
    return {
      queryParams: {
        p: 1,
        s: 10,
        name: ''
      },
      tmpName: '',
      reportList: [],
      total: 0,
      reportContentDialogVisible: false,
      reportHtml: ''
    }
  },
  methods: {
    // 获取报告列表
    getReportList () {
      if (this.queryParams.name !== this.tmpName) {
        this.tmpName = this.queryParams.name
        this.queryParams.p = 1
      }
      getReports(this.queryParams).then(response => {
        if (response.data.code !== 0) return this.$message.error('获取报告列表失败！')
        this.reportList = response.data.data.results
        this.total = response.data.data.count
      })
    },
    // 打开报告
    openReportHtml (reportId) {
      this.reportContentDialogVisible = true
      getReportHtml(reportId).then(response => {
        if (response.data.code !== 0) return this.$message.error('获取报告详情失败！')
        this.reportHtml = response.data.data.content
      })
    },
    reportContentClose () {
      this.reportHtml = ''
    },
    reportContentDialogClose () {
      this.reportContentDialogVisible = false
    },
    // 下载报告
    downloadReportHtml (reportId, reportName) {
      this.$confirm('此操作将下载该测试报告, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        downloadReportHtml(reportId).then(response => {
          if (response.status !== 200) return this.$message.error('下载失败！')
          const url = window.URL.createObjectURL(new Blob([response.data]))
          const linka = document.createElement('a')
          linka.href = url
          linka.setAttribute('download', reportName + '.html')
          document.body.appendChild(linka)
          linka.click()
          linka.remove()
        })
      }).catch(err => err)
    },
    // 监听size变化
    handleSizeChange (newSize) {
      this.queryParams.s = newSize
      this.getReportList()
    },
    // 监听页码变化
    handleCurrentChange (newPage) {
      this.queryParams.p = newPage
      this.getReportList()
    },
    // 表格样式
    cellStyle (data) {
      if (data.column.label === '报告名称') {
        return 'color: #409EFF'
      }
      if (data.column.label === '运行结果') {
        if (data.row.result === 'succeed') {
          return 'color: #67C23A'
        } else {
          return 'color: #F56C6C'
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .report {
    margin-top: -20px;
    overflow-y: scroll;
    white-space: nowrap;
  }
  /deep/ .popup {
    height: 100%;
  }
  /deep/ .popup .content {
    overflow-y: scroll;
    white-space: nowrap;
  }
  /deep/ .el-dialog__body {
    padding: 20px 35px 10px 20px;
  }
</style>
